package com.yuyou.fn.provider.impl;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.yuyou.fn.api.CustomerUserService;
import com.yuyou.fn.api.dto.CustomerUserDto;
import com.yuyou.fn.api.dto.StudentDto;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.service.ISmsService;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.educational.entity.CustomerBindStudent;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.service.ICustomerBindStudentService;
import com.yuyou.fn.educational.service.ICustomerUserService;
import com.yuyou.fn.educational.service.IStudentService;
import com.yuyou.fn.educational.vo.CustomerBindStudentVo;
import com.yuyou.fn.platform.entity.CustomerUser;
import org.apache.commons.lang3.Validate;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ExposeCustomerUserServiceImpl implements CustomerUserService {

    @Resource
    private ICustomerUserService customerUserService;

    @Resource
    private ISmsService smsService;

    @Resource
    private IStudentService studentService;

    @Resource
    private ICustomerBindStudentService customerBindStudentService;


    @Override
    public CustomerUserDto login(String phoneNo, String code) {


        if( !smsService.validate(phoneNo, code) ){
            throw new BusException("手机验证码不正确");
        }
        CustomerUser customerUser = customerUserService.loginByPhoneNo(phoneNo);
        CustomerUserDto customerUserDto = toCustomerUserDto(customerUser);
        return customerUserDto ;
    }



    @Override
    public CustomerUserDto findByUserId(Long userId) {
        return toCustomerUserDto(customerUserService.findById(userId));
    }

    @Override
    public boolean updateCustomerUserInfo(Long userId, String password, String phoneNo, String avatarUrl, Integer sex, String location) {
        Validate.notNull(userId);
        CustomerUser customerUser = new CustomerUser();
        customerUser.setUserId(userId);
        customerUser.setPhoneNo(phoneNo);
        customerUser.setAvatarUrl(avatarUrl);
        customerUser.setSex(sex);
        customerUser.setLocation(location);
        customerUserService.updatePersonMessage(customerUser);
        return true;
    }

    @Override
    public Collection<StudentDto> findBindStudent(Long userId) {
        List<CustomerBindStudentVo> customerBindStudentVos = customerBindStudentService.findStudentsByUserIds(userId);
        if(customerBindStudentVos == null ){
            return Collections.EMPTY_LIST;
        }
        List<Long> studentIds = BeanUtils.toList(customerBindStudentVos, "studentId");

        List<Student> students = studentService.findByIds(studentIds);

        return Collections2.transform(students, new Function<Student, StudentDto>() {
            @Override
            public StudentDto apply(Student student) {
                return toStudentDto(student);
            }
        });
    }


    @Override
    public boolean bindStudent(Long userId, Long studentId) {
        customerBindStudentService.insertByStudentId(studentId,userId);
        return true;
    }

    @Override
    public boolean unbindStudent(Long userId, Long studentId) {
        customerBindStudentService.deleteByStudentIdAndUserId(studentId,userId);
        return true;
    }

    @Override
    public boolean isBindStudent(Long userId, Long studentId) {
        CustomerBindStudent customerBindStudent = customerBindStudentService.findByStudentIdAndUserId(studentId, userId);
        return customerBindStudent == null ? false : true ;
    }

    private CustomerUserDto toCustomerUserDto(CustomerUser customerUser) {
        CustomerUserDto customerUserDto = new CustomerUserDto();
        BeanUtils.copyProperties(customerUserDto,customerUser);
        return customerUserDto;
    }

    private StudentDto toStudentDto(Student student) {
        StudentDto studentDto = new StudentDto();
        BeanUtils.copyProperties(studentDto,student);
        return studentDto;
    }
}
